package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.service.AdminAdService;
import com.cskaoyan.market.service.impl.AdminAdServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;



@WebServlet("/admin/ad/*")
public class AdminAdController extends HttpServlet {
    AdminAdService adminAdService = new AdminAdServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/ad/", "");

        if("create".equals(op)) {
            create(req, resp);
        }
        if ("update".equals(op)){
            update(req, resp);
        }
        if ("delete".equals(op)){
            delete(req, resp);
        }


    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = req.getReader().readLine();
        MarketAd marketAd = objectMapper.readValue(s, MarketAd.class);

        int i = adminAdService.update(marketAd);

        if (i==1){
            Object ok = ResponseUtil.ok(marketAd);
            String s1 = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(s1);
            return;
        }

        Object fail = ResponseUtil.fail(762, "更新失败");
        String s1 = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(s1);


    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = req.getReader().readLine();
        MarketAd marketAd = objectMapper.readValue(s, MarketAd.class);

         int i =adminAdService.create(marketAd);

         if (i==1){
             Object ok = ResponseUtil.ok(marketAd);
             String s1 = JacksonUtil.getObjectMapper().writeValueAsString(ok);
             resp.getWriter().write(s1);
             return;
         }

        Object fail = ResponseUtil.fail(764, "插入失败");
        String s1 = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(s1);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op =requestURI.replace(req.getContextPath()+"/admin/ad/","");

        if ("list".equals(op)){
            list(req, resp);
        }

    }




    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String op = req.getReader().readLine();
        MarketAd marketAd = objectMapper.readValue(op, MarketAd.class);

        int i =adminAdService.delete(marketAd);
        if (i==1){
            Object ok = ResponseUtil.ok();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(s);
            return;
        }

        Object fail = ResponseUtil.fail(763, "删除失败");
        String s = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(s);

    }


    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String link = req.getParameter("link");
        String url = req.getParameter("url");
        String position = req.getParameter("position");
        String content = req.getParameter("content");
        String addTime = req.getParameter("add time");
        String updateTime = req.getParameter("update time");
        String endTime = req.getParameter("end time");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");


        List<MarketAd>marketAdList =  adminAdService.insert(id,name,link,url,position,content,position,endTime,updateTime , page,limit);


        Object o = ResponseUtil.okList(marketAdList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);

    }



}

